package grg.learn.demoframework

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.Divider
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import kotlinx.coroutines.flow.flow

abstract class ItemModelBuildScope<T : ItemModel> {
    private val items = ArrayList<T>()

    internal fun build(): List<T> {
        return items
    }

    internal fun clear() {
        items.clear()
    }

    infix fun String.has(runnable: Runnable?) {
        items.add(makeModel(this, runnable = runnable))
    }

    abstract fun makeModel(string: String, runnable: Runnable?): T

    companion object : ItemModelBuildScope<ItemModel>() {
        override fun makeModel(string: String, runnable: Runnable?): ItemModel {
            return ItemModel(string, runnable)
        }
    }

}

data class ItemModel(val label: String, val call: Runnable? = null)

/**
 * create by iso88591 at 2022/3/4
 *
 * use like:

ActionList(scope = {
"" has {

}

"" has {

}
})

 *
 *
 * @see [DisplayItem] 来自定义item
 *
 */
fun <T : ItemModel> ItemModelBuildScope<T>.buildActionList(
    scope: ItemModelBuildScope<T>.() -> Unit,
): List<T> {
    this.clear()
    return this.apply(scope).build()
}

fun buildActionList(scope: ItemModelBuildScope<ItemModel>.() -> Unit) =
    ItemModelBuildScope.buildActionList(scope = scope)

@Composable
fun <T : ItemModel, L : List<T>> L.ComposeListAction(DisplayItem: IItemAction = IItemAction) {
    LazyColumn(content = {
        items(size) {
            val model = this@ComposeListAction[it]
            DisplayItem(model = model)
        }
    })
}

interface IItemAction {

    @Composable
    fun DisplayActionItem(model: ItemModel)

    @Composable
    operator fun invoke(model: ItemModel) = DisplayActionItem(model = model)

    companion object : IItemAction {


        @Composable
        private fun ComposeItem(modifier: Modifier, model: ItemModel) {

            Box(
                modifier = modifier
                    .fillMaxWidth()
                    .height(50.dp)
                    .padding(start = 15.dp),
                contentAlignment = Alignment.CenterStart
            ) {
                Text(text = model.label, fontSize = 15.sp)
            }

            Divider()

        }

        @Composable
        override fun DisplayActionItem(model: ItemModel) {
            ComposeItem(modifier = Modifier.clickable { model.call?.run() }, model = model)
        }

        inline operator fun invoke(crossinline displayComposeBlock: @Composable ItemModel.() -> Unit): IItemAction {
            return object : IItemAction {
                @Composable
                override fun DisplayActionItem(model: ItemModel) {
                    displayComposeBlock(model)
                }
            }
        }

    }

}

//@Preview()
//@Composable
//fun test() {
//    buildActionList {
//
//        "1" has {
//
//        }
//
//        "2" has {
//
//        }
//
//    }.ComposeListAction(IItemAction {
//        Text(text = "")
//    })
//}